package com.lt.test.consumer.web.controller;

import com.alibaba.dubbo.config.annotation.Reference;
import com.lt.test.api.RoleService;
import com.lt.test.common.constant.HttpRequestConstant;
import com.lt.test.common.exception.CustomException;
import com.lt.test.entity.DO.RoleDO;
import com.lt.test.entity.DTO.CustomRole;
import com.lt.test.entity.DTO.PageDTO;
import com.lt.test.entity.DTO.ResponseDTO;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.ResponseBody;
import org.springframework.web.bind.annotation.RestController;

import javax.annotation.Resource;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import java.util.List;
import java.util.Map;

/**
 * Created by zhengchuan on 2017/3/27.
 */
@RestController
@RequestMapping("/sys/role")
public class RoleController {
    private static final Logger logger = LoggerFactory.getLogger(RoleController.class);
    @Autowired
    private RoleService roleService;

    /**
     * 分页用户信息
     *
     * @param request
     * @param response
     * @return
     */
    @RequestMapping("/getRoleInfo")
    public
    PageDTO<CustomRole> getRoleInfo(HttpServletRequest request, HttpServletResponse response) {
        Integer page = Integer.valueOf(request.getParameter("page"));
        Integer pageSize = Integer.valueOf(request.getParameter("rows"));
        String sortedKey = request.getParameter("sidx");
        String sortedValue = request.getParameter("sord");
        String filters = request.getParameter("filters");
        PageDTO<CustomRole> result = null;
        try {
            result = roleService.findUserByPage(page, pageSize, sortedKey, sortedValue, filters);
        } catch (CustomException e) {
            logger.error("获取角色列表失败");
            e.printStackTrace();
            result = new PageDTO<>();
        }
        return result;
    }


    /**
     * 获取角色的select用来展示
     * @return
     */
    @RequestMapping(value = "/getRoleInfoToSelect", produces = "application/json; charset=utf-8")
    public String getRoleForSelect() {
        try {
            return roleService.getRoleForSelect();
        } catch (CustomException e) {
            e.printStackTrace();
        }
        return "<select><option value='-999'>获取角色列表失败</option></select>";
    }

    /**
     * 获取角色的List
     * @return
     */
    @RequestMapping(value = "/getRoleForList", produces = "application/json; charset=utf-8")
    public @ResponseBody
    ResponseDTO getRoleForList() {
        ResponseDTO responseDTO = new ResponseDTO();
        try {

            List list = roleService.getRoleForList();
            responseDTO.setData(list);
            responseDTO.setSuccess(true);
            responseDTO.setCode(HttpRequestConstant.REQUEST_SUCCESS_CODE);
            responseDTO.setMessage("操作成功");
        } catch (CustomException e) {
            e.printStackTrace();
            responseDTO.setCode(HttpRequestConstant.SERVER_ERROR_CODE);
            responseDTO.setMessage(e.getMessage());
        }
        return responseDTO;
    }

    @RequestMapping("/editRoleInfo")
    public
    ResponseDTO editRoleInfo(HttpServletRequest request, HttpServletResponse response, CustomRole role) {
        ResponseDTO responseDTO = new ResponseDTO();
        String oper = request.getParameter("oper");
        try {
            RoleDO roleDO1 = roleService.findRoleByRoleKey(role.getRoleKey());
            if (roleDO1 != null && !roleDO1.getId().equals(role.getId())) {
                responseDTO.setMessage("角色Key: " + role.getRoleKey() + " 已存在");
                return responseDTO;
            }
            RoleDO roleDO2 = roleService.findRoleByRoleName(role.getRoleName());
            if (roleDO2 != null && !roleDO2.getId().equals(role.getId())) {
                responseDTO.setMessage("角色名称: " + role.getRoleName() + " 已存在");
                return responseDTO;
            }

            roleService.editRoleInfo(role,oper);
            responseDTO.setSuccess(true);
            responseDTO.setCode(HttpRequestConstant.REQUEST_SUCCESS_CODE);
            responseDTO.setMessage("操作成功");
        } catch (CustomException e) {
            responseDTO.setCode(HttpRequestConstant.SERVER_ERROR_CODE);
            responseDTO.setMessage(e.getMessage());
            e.printStackTrace();
        } catch (Exception e) {
            responseDTO.setCode(HttpRequestConstant.SERVER_ERROR_CODE);
            responseDTO.setMessage("未知错误");
            e.printStackTrace();
        }
        return responseDTO;

    }

    @RequestMapping("/getPrivilegesByRoleId")
    public
    ResponseDTO getPrivilegesByRoleId(HttpServletRequest request, HttpServletResponse response, String roleId) {
        ResponseDTO responseDTO = new ResponseDTO();
        try {
            List<Map<String,Object>> result = roleService.getPrivilegesByRoleId(roleId);
            responseDTO.setSuccess(true);
            responseDTO.setData(result);
            responseDTO.setCode(HttpRequestConstant.REQUEST_SUCCESS_CODE);
            responseDTO.setMessage("操作成功");
        } catch (CustomException e) {
            responseDTO.setCode(HttpRequestConstant.SERVER_ERROR_CODE);
            responseDTO.setMessage(e.getMessage());
            e.printStackTrace();
        } catch (Exception e) {
            responseDTO.setCode(HttpRequestConstant.SERVER_ERROR_CODE);
            responseDTO.setMessage("未知错误");
            e.printStackTrace();
        }
        return responseDTO;

    }

    @RequestMapping("/roleAuthorization")
    public
    ResponseDTO roleAuthorization(String roleId, String[] checkedNode ,String[] cancelCheckedNode) {
        ResponseDTO responseDTO = new ResponseDTO();
        try {
            roleService.roleAuthorization(roleId,checkedNode,cancelCheckedNode);
            responseDTO.setSuccess(true);
            responseDTO.setCode(HttpRequestConstant.REQUEST_SUCCESS_CODE);
            responseDTO.setMessage("授权成功");
        } catch (CustomException e) {
            responseDTO.setCode(HttpRequestConstant.SERVER_ERROR_CODE);
            responseDTO.setMessage(e.getMessage());
            e.printStackTrace();
        } catch (Exception e) {
            responseDTO.setCode(HttpRequestConstant.SERVER_ERROR_CODE);
            responseDTO.setMessage("未知错误");
            e.printStackTrace();
        }
        return responseDTO;

    }

}
